/* -----------------------------------------------------------------------------
 * PROJECT    : net.lwf
 * FILE       : fElapsedTimeHandler.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Jan 26, 2009       Raghu Sarraff			       Created
 */
package net.lwf.handler.time;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;

import lwf.handler.IProcessHandler;
import lwf.handler.ProcessHandler;
import lwf.handler.entities.RadioData;
import lwf.handler.helper.DisplayFormatter;
import net.lwf.common.constant.ICommonConstant;
import net.lwf.entity.QuestionInfo;
import net.lwf.handler.time.util.Time;
import net.lwf.handler.time.util.TimeGenerator;
import net.lwf.handler.time.util.TimeUtil;
import net.lwf.number.util.RandomNumberGenerator;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;

public class ElapsedTimeHandler extends ProcessHandler implements
		IProcessHandler {
	
	public ElapsedTimeHandler() {
	}	
	
	/**
	 * @see net.lwf.handler.IProcessHandler#getDisplayFormatter()
	 */
	public DisplayFormatter getDisplayFormatter() {
		return null;
	}

	/**
	 * @see net.lwf.handler.IProcessHandler#getQuestionDesc()
	 */
	public String getQuestionDesc() {
		return null;
	}
	
	private String generateHtmlQuestion(Time time1, Time time2) {
		StringBuffer sbQBlock = new StringBuffer();
		sbQBlock.append("<table border=\"0\" width=\"100%\" align=\"center\" cellspacing=\"2\" cellpadding=\"0\">");

        	sbQBlock.append("<tr>");
	   			sbQBlock.append("<td width=\"40%\" class=\"datacenter\" nowrap>");
					sbQBlock.append("<u>Time 1</u>");
				sbQBlock.append("</td>");        
				sbQBlock.append("<td width=\"20%\" > &nbsp; </td>");
	       		sbQBlock.append("<td width=\"40%\" class=\"datacenter\" nowrap>");
	       			sbQBlock.append("<u>Time 2</u>");
				sbQBlock.append("</td>");
			sbQBlock.append("</tr>");
        	sbQBlock.append("<tr>");
       			sbQBlock.append("<td width=\"40%\" class=\"datacenter\" nowrap>");
       				sbQBlock.append("<div class=\"time_clock\" >");
       					sbQBlock.append("<img src=\"" + time1.getImagePath() + "\" >");
					sbQBlock.append("</div>");
					sbQBlock.append("<br>");
					sbQBlock.append(time1.getFormattedTime());
				sbQBlock.append("</td>");        
				sbQBlock.append("<td width=\"20%\" > &nbsp; </td>");
	       		sbQBlock.append("<td width=\"40%\" class=\"datacenter\" nowrap>");
	       			sbQBlock.append("<div class=\"time_clock\" >");
	       				sbQBlock.append("<img src=\"" + time2.getImagePath() + "\" >");
	       			sbQBlock.append("</div>");
	       			sbQBlock.append("<br>");
	       			sbQBlock.append(time2.getFormattedTime());
				sbQBlock.append("</td>");
        	sbQBlock.append("</tr>");
        	sbQBlock.append("<tr>");
				sbQBlock.append("<td colspan=\"3\"> &nbsp; </td>");
			sbQBlock.append("</tr>");
			
        sbQBlock.append("</table>");
        
        return sbQBlock.toString();	
	}
	
	//------------------------------------------------ Methods for PDF generation on the browser
	public void generate(Document document) {
	
	}

	@Override
	public List<QuestionInfo> generateMultiple() {
		return null;
	}

	@Override
	public QuestionInfo generateSingle() {
		return this.generateSingle(false);
	}
	
	public QuestionInfo generateSingle(boolean isMultiple) {
		QuestionInfo qinfo = new QuestionInfo();
		String answer = null;
		Time time1 = TimeGenerator.generateTime(true, true, false);
		Time time2 = TimeGenerator.generateTime(true, true, false);
		
		qinfo.setHtmlQuestion(this.generateHtmlQuestion(time1, time2));
		qinfo.setPlaceHolder("Compare the following times. Which time occurs first and what is the elapsed time?");
		
		long elapsed = TimeUtil.getElapsedTime(time1, time2);
		if(elapsed < 0) {
			elapsed = -(elapsed);
			answer = "Time 2, ";
		}
		else {
			answer = "Time 1, ";
		}
		Time time = new Time(0, 0, elapsed);
		long hours = time.getHours();
		//if(hours > 12) {
			//hours = 24 - 12;
		//}
		answer += String.valueOf(hours) + ICommonConstant.COLON + String.valueOf(time.getMinutes()); 
		qinfo.setAnswer(answer);
		//Set radio answers
		qinfo.setRadioAnswerChoices(this.getRadioAnswers(answer));
		
		return qinfo;
	}
	
	private List<RadioData> getRadioAnswers(String answer) {
		List<RadioData> rdata = new ArrayList<RadioData>();
		
		
		//Time 2, 9:45
		StringTokenizer stk = new StringTokenizer(answer, ICommonConstant.COMMA);
		String timer = stk.nextToken();
		StringTokenizer stk_time = new StringTokenizer(stk.nextToken(), ICommonConstant.COLON);
		int hour = Integer.parseInt(stk_time.nextToken().trim());
		int minute = Integer.parseInt(stk_time.nextToken().trim());
		String displayAnswer = timer + ", " + String.valueOf(hour) + " hours and " + String.valueOf(minute) + " minutes";
		
		rdata.add(new RadioData(answer, displayAnswer));
		
		List<Integer> hour_choices = RandomNumberGenerator.generateIntsWithVariance(hour, 12, 3, 3);
		List<Integer> minute_choices = RandomNumberGenerator.generateIntsWithVariance(minute, 60, 3, 3);
			
		for(int i=0; i<hour_choices.size(); i++) {
			String answer_choice = "";
			int rand = RandomNumberGenerator.generate(1, 2);
			if(rand == 1) {
				answer_choice += "Time 1, ";
			}
			else {
				answer_choice += "Time 2, ";
			}
			answer_choice += timer + ", " + String.valueOf(hour_choices.get(i)) + " hours and " + String.valueOf(minute_choices.get(i)) + " minutes";
			rdata.add(new RadioData(answer_choice, answer_choice));
		}
		
		Collections.shuffle(rdata);
		
		return rdata;
	}

	@Override
	public int getColumns() {
		return 0;
	}

	@Override
	public void getContent(Document document) throws DocumentException {
	}

	@Override
	public int getRows() {
		return 0;
	}

	@Override
	public boolean isPortrait() {
		return false;
	}

	@Override
	public String getPdfInstructionalText() {
		return null;
	}

	@Override
	public String getPdfNote() {
		return null;
	}

}

